<template>
  <el-dialog :model-value="dialogVisible" title="分配填报权限" width="800px" :before-close="handleClose" append-to-body
    custom-class="scope-dialog">
    <OrgWorkPeople ref="orgRef" :scopeInfo="scopeInfo" v-if="isShow" @on-update="changeData">
    </OrgWorkPeople>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import OrgWorkPeople from '@/components/OrgWorkPeople/index.vue'
import { updateData, submitDataHanle, queryPermisson } from './common'
import { onMounted, reactive } from 'vue'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },

    selectList: {
      type: Array,
      default: () => []
    }
  },
  emits: ['handleClose', 'on-update'],
  setup(props, { emit }) {
    const orgRef = ref(null)

    const state = reactive({
      orgRef: null,
      submitData: [],
      scopeInfo: '',
      isShow:false
    })
    const methods = {
      changeData([data, text]) {
        updateData([data, text], props.selectList, state)
      },
      handleClose() {
        emit('handleClose')
      },
 
      handleSubmit() {
        state.orgRef.submit()
        submitDataHanle(methods, emit, state.submitData,'update')
      },
      changeData([data, text]) {
        updateData([data, text], props.selectList, state)
      },
      async getWiterFormData() {
        let formCodes = props.selectList.map(item => item.formCode)
        const result = await queryPermisson(formCodes, props.selectList)
        let positionsIds = []
        let membersIds = []
        let companiesIds = []
        let userList = []
        console.log(result,'resultresultresult')
        if (result && result.length == 1) {
          result[0].tags.forEach(item => {
            if (item.filterType == 'member') {
              membersIds.push(item.filterId)
              userList.push({
                email: item.filterCode,
                id: item.filterId,
                isSelect: true,
                label: item.filterName,
                name: item.filterName,
                sourceType: "user",
              })
            }
            if (item.filterType == 'organization') companiesIds.push(Number(item.filterId))
            if (item.filterType == 'role') positionsIds.push(item.filterId)

          })
          state.scopeInfo = {
            positionsIds,
            membersIds,
            companiesIds,
            userList

          }
          state.isShow = true
          console.log(state.scopeInfo, 'state.scopeInfo')
        }
 
      }

    
    }
    onMounted(() => {
      methods.getWiterFormData()
    })
    return {
      ...toRefs(state),
      ...methods
    }



  }
}


</script>

<style >
.scope-dialog .el-dialog__body {
  margin-bottom: 0;
  padding-bottom: 0;
}
</style>
